home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / comm / tcp / rxsocket.lha / rxsocket / examples / amiget / amiget.rexx < prev    next >
Encoding:
OS/2 REXX Batch file  |  1998-10-27  |  8.8 KB  |  327 lines

  1. /**amiget/2.0 by alfie**/
  2. /**libs**/
  3. signal on break_c
  4. if ~show("L","rexxsupport.library") then
  5.     if ~addLib("rexxsupport.library",0,-30) then
  6.         call serr("can't find rexxsupport.library")
  7. if ~show("L","rxsocket.library") then
  8.     if ~addLib("rxsocket.library",0,-30) then
  9.         call serr("can't find rxsocket.library")
  10. if ~show("L","rmh.library") then
  11.     if ~addLib("rmh.library",0,-30) then
  12.         call serr("can't find rmh.library")
  13. if ~show("L","rxwiz.library") then
  14.     if ~addLib("rxwiz.library",0,-30) then
  15.         call serr("can't find rxwiz.library"
  16. if ~show("L","rxasl.library") then
  17.     if ~addLib("rxasl.library",0,-30) then
  18.         call serr("can't find rxasl.library")
  19. /**args**/
  20. global.amirc=left(address(),6)=="AMIRC."
  21. parm.5.value=""
  22. template="HOST/A,FILE/A,AS/K,PORT/K/N,TIMEOUT/K/N,PUBSCREEN/K,LOGIN/K,PASS/K,HELP/S,PROXY/K,PROXYPORT/K/N,NOCACHE=NC/S,DEBUG=DB/S,INFO/S,RESUME/S"
  23. if ~RMH_ReadArgs(template) then
  24.     call serr(Fault(IoErr(),"amiget"))
  25. if parm.8.flag then call serr(template)
  26. if parm.4.flag & parm.4.value<=0 then
  27.     call serr("timeout must be greater than 0")
  28. if parm.6.flag~=parm.7.flag then
  29.     call serr("you must supply both login and password")
  30. if parm.3.flag then do
  31.     port=parm.3.value
  32.     if port<0 | port>65535 then call serr("bad port number")
  33. end
  34. else port=80
  35. if parm.9.flag then do
  36.     if parm.10.flag then proxyport=parm.10.value
  37.     else proxyport=8080
  38.     if proxyport<0 | proxyport>65535 then call serr("bad proxy port number")
  39. end
  40. if parm.2.flag then do
  41.     range=""
  42.     saveDir=PathPart(parm.2.value)
  43.     saveFile=FilePart(parm.2.value)
  44.     if saveDir~="" then do
  45.         if word(statef(saveDir),1)~="DIR" then call serr(Fault(IoErr(),"amiget"))
  46.         if saveFile=="" then saveFile=AddPart(saveDir,FilePart(parm.1.value))
  47.         else saveFile=parm.2.value
  48.     end
  49.     else saveFile=parm.2.value
  50.     if parm.14.flag & exists(saveFile) then
  51.         range="Range: "word(statef(saveFile),2)+1"-"
  52.  
  53. end
  54. else if parm.14.flag then call serr("for resuming you must specify a file")
  55. /*is a bsdsocket.library present?*/
  56. if ~IsLibOn("SOCKET") then do
  57.     call EasyRequest("No Internet stack is running.","amiget",,parm.5.value)
  58.     exit
  59. end
  60. /*window*/
  61. global.sock=-1
  62. sur.SnapShot=1
  63. res=OpenSurface("rexx:amiget.wizard","SUR")
  64. if res~=0 then call serr(GetRxWizString(res))
  65. if parm.5.flag then win.PubScreen=parm.5.value
  66. win.Title="Amiget" FilePart(parm.1.value)
  67. win.Gads=5
  68. win.ID=1
  69. res=OpenWindow("SUR","WIN")
  70. if res~=0 then call serr(GetRxWizString(res))
  71. global.ws=WindowSignal("SUR","WIN")
  72. global.hws=0
  73. /**socket**/
  74. call SetSocketSignals(or(global.ws,2**12))
  75. if parm.9.flag then do
  76.     sin.addrPort=proxyport
  77.     sin.addrAddr=Resolve(parm.9.value)
  78. end
  79. else do
  80.     sin.addrPort=port
  81.     sin.addrAddr=Resolve(parm.0.value)
  82. end
  83. if sin.addrAddr==-1 then call err("Can't find host",1)
  84. sin.addrFamily="INET"
  85. global.sock=socket("INET","STREAM")
  86. if global.sock<0 then call err("No socket")
  87. /**connection**/
  88. call info("Connecting...")
  89. if parm.4.flag then do
  90.     tim=CreateTimer()
  91.     ts=TimerSignal(tim)
  92.     call SetSocketSignals(or(global.ws,ts,2**12))
  93.     call StartTimer(tim,parm.4.value)
  94. end
  95. res = connect(global.sock,"SIN")
  96. err=errno()
  97. if (res<0) && (err~=35) && (err~=36) then call err("Can't connect")
  98. /**request**/
  99. call info("Sending request...")
  100. if range~="" then httpV = "HTTP/1.1"
  101. else httpV = "HTTP/1.0"
  102. if left(parm.1.value,1)~="/"  then parm.1.value="/"parm.1.value
  103. if parm.9.flag then req="GET http://"parm.0.value":"port || parm.1.value httpV
  104. else req="GET" parm.1.value httpV
  105. hst="Host:" parm.0.value
  106. usr="User-Agent: amiget/2.0 (AmigaOS - ARexx - rxsocket.library)"
  107. if parm.6.flag then auth="Authorization: Basic" encodeB64(parm.6.value":"parm.7.value)
  108. else auth=""
  109. if parm.11.flag then cache="Pragma: no-cache"
  110. else cache=""
  111. if parm.12.flag then do
  112.     call print "Sending:"
  113.     call print req
  114.     call print usr
  115.     call print range
  116.     call print auth
  117.     call print cache
  118.     call print "-"
  119. end
  120. req=req"D0A"x ||  usr"D0A"x
  121. if auth~="" then req=req||auth"D0A"x
  122. if cache~="" then req=req||cache"D0A"x
  123. if hst~="" then req=req||hst"D0A"x
  124. if range~="" then req=req||range"D0A"x
  125. req=req"D0A"x
  126. if send(global.sock,req)~=length(req) then call err("Send error")
  127. /**head**/
  128. call info("Receiving and parsing head...")
  129. len=recvline(global.sock,"BUF",256)
  130. if len<0 then call err("Recv error")
  131. if left(buf,5)~="HTTP/" then call err("Bad answer from server")
  132. parse var buf "HTTP/"ver code coderest"D0A"x
  133. go=1
  134. length=""
  135. last=""
  136. mime=""
  137. hs="HTTP/"ver code coderest"A"x
  138. do while go
  139.     len=RecvLine(global.sock,"BUF",256)
  140.     if len<0 then call err("Error receiving")
  141.     parse var buf b": "rest"D0A"x
  142.     select
  143.         when b=="Content-Length" then length=rest
  144.         when b=="Last-Modified" then last=rest
  145.         when b=="Content-type" then mime=rest
  146.         when b=="WWW-authenticate" then auth=rest
  147.         otherwise nop
  148.     end
  149.     go=(buf~="D0A"x)
  150.     if go then do
  151.         if parm.12.flag then call print b":" rest
  152.         hs=hs || b":" rest || "A"x
  153.     end
  154. end
  155. if parm.4.flag then call FreeTimer(tim)
  156.  
  157. if parm.13.flag then do
  158.     if parm.5.flag then headwin.PubScreen=parm.5.value
  159.     headwin.Title="Amiget" FilePart(parm.1.value)
  160.     headwin.Gads=5
  161.     headwin.ID=2
  162.     res=OpenWindow("SUR","HEADWIN")
  163.     if res~=0 then call serr(GetRxWizString(res))
  164.     global.hws=WindowSignal("SUR","HEADWIN")
  165.     set.STRINGFIELDTEXT=hs
  166.     res=SetWizAttrs("SUR","HEADWIN","HEADFIELD","SET");drop set.
  167.     if res~=0 then call serr(GetRxWizString(res))
  168. end
  169.  
  170. if code~=200 then
  171.     if parm.14.flag & code~=206 then call err("Server error:" code coderest)
  172. call SetSocketSignals(0)
  173. if length~="" then l=l || length
  174. else l=l || "unknown"
  175. if length~="" then do
  176.     set.GaugeTotal=length
  177.     call SetWizAttrs("SUR","WIN","PROGRESS","SET");drop set.
  178. end
  179. call IOCtlSocket(global.sock,"FIONBIO",0)
  180. call SetSockOpt(global.sock,"SOCKET","EVENTMASK","READ")
  181. /**savefile**/
  182. if ~parm.2.flag then do
  183.     call LockWindows("SUR")
  184.     if parm.5.flag then req.PubScreen=parm.5.value
  185.     req.DoSaveMode=1
  186.     req.file=FilePart(parm.1.value)
  187.     if ReqFile("REQ")==0 & req.file~="" then do
  188.         call UnLockWindows("SUR")
  189.         saveFile=AddPart(req.drawer,req.file)
  190.     end
  191.     else exit
  192. end
  193. if range=="" then res=Open("OUT",saveFile,"W")
  194. else res=Open("OUT",saveFile,"A")
  195. if ~res then call err("Can't open file")
  196. /**body**/
  197. dummyL=200
  198. dummyS=copies(" ",dummyL)
  199. tl=0
  200. cps=0
  201. go=1
  202. sel.read.0=global.sock
  203. call time("R")
  204. stal=0
  205. do while go
  206.     res = WaitSelect("SEL",0,800000,or(global.ws,global.hws))
  207.     et=time("E")
  208.     if and(sel.signals,global.ws)~=0 then exit
  209.     if and(sel.signals,global.hws)~=0 then call CloseWindow("SUR","HEADWIN")
  210.     ets="ETime:" etime(et)
  211.     if res==0 then do
  212.         stal=stal+1
  213.         if stal>5 then do
  214.             set.GaugeFormat=ets "stalled at" tl "bytes"
  215.         end
  216.     end
  217.     else do
  218.         stal=0
  219.         len=recv(global.sock,"BUF",1024)
  220.         if len<0 then do
  221.             call err("Error Receiving")
  222.             exit
  223.         end
  224.         if len~=0 then do
  225.             if WriteCH("OUT",buf)~=length(buf) then call err("Error writing")
  226.             tl=tl+len
  227.         end
  228.         else go=0
  229.     end
  230.     if stal<=5 then do
  231.         if tl~=0 & et~=0 then cps=(tl/et)%1
  232.         if length~="" then do
  233.             set.GaugeCurrent=tl
  234.             call SetWizAttrs("SUR","WIN","PROGRESS","SET");drop set.
  235.             set.GaugeFormat=ets "Size:" tl || "/"length "(" || (tl*100)%length"%%)"
  236.         end
  237.         else set.GaugeFormat=ets "-" tl "(lenght unknown)"
  238.         set.GaugeFormat=set.GaugeFormat "at" cps "cps"
  239.     end
  240.     set.GaugeFormat=left(set.GaugeFormat||dummyS,dummyL)
  241.     res=SetWizAttrs("SUR","WIN","PROGRESS","SET");drop set.
  242. end
  243. et=time("E")
  244. call CloseSocket(global.sock)
  245. if length~="" then
  246.     if tl~=length then call err("Aspected" length "- received" tl "bytes")
  247. set.Label="Exit"
  248. res=SetWizAttrs("SUR","WIN","ABORT","SET");drop set.
  249. stop=0
  250. do while ~stop
  251.     sigrec=Wait(or(global.ws,global.hws))
  252.     if and(sigrec,global.hws)~=0 then call CloseWindow("SUR","HEADWIN")
  253.     else stop=1
  254. end
  255. exit
  256. /**info**/
  257. info: procedure expose global.
  258. parse arg msg
  259.     set.GaugeFormat=msg
  260.     res=SetWizAttrs("SUR","WIN","PROGRESS","SET")
  261.     return
  262.  
  263. /**out**/
  264. print: procedure expose global.
  265. parse arg msg
  266.     if global.amirc then
  267.         "ECHO P=" || "1B"x || "bĀ«AmigetĀ» C=6" msg
  268.     else say msg
  269.     return
  270. /**nowin errors**/
  271. serr: procedure expose global.
  272. parse arg msg
  273.     call print msg
  274.     exit
  275. /**errors**/
  276. err: procedure expose global.
  277. parse arg s,res
  278.     if global.sock>=0 then call CloseSocket(global.sock)
  279.     if res==1 then do
  280.         if ~IsLibOn("TTCP") then s=s "("HostErrorString(hosterrorno())")"
  281.     end
  282.     else do
  283.         err=errno()
  284.         if err==4 then do
  285.             s="User break or timeout"
  286.             err=0
  287.         end
  288.         if err~=0 then
  289.         if IsLibOn("TTCP") then s=s "("err")"
  290.         else s=s "("ErrorString(err)")"
  291.     end
  292.     set.Label="Exit"
  293.     res=SetWizAttrs("SUR","WIN","ABORT","SET")
  294.     call info(s)
  295.     stop=0
  296.     do while ~stop
  297.         sigrec=Wait(or(global.ws,global.hws))
  298.         if and(sigrec,global.hws)~=0 then call CloseWindow("SUR","HEADWIN")
  299.         else stop=1
  300.     end
  301.     exit
  302. /**break_c**/
  303. BREAK_C:
  304.     exit
  305. /**B64**/
  306. encodeB64: procedure
  307. parse arg s
  308.     s=c2b(s)
  309.     a=""
  310.     do while s~=""
  311.         parse var s c +6 s
  312.         a=a||substr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",c2d(b2c(left(c"0000",6)))+1,1)
  313.     end
  314.     l=length(c)
  315.     if l<6 then a=a||copies("=",(6-l)/2)
  316.     return a
  317. /**etime**/
  318. etime: procedure
  319. parse arg s
  320.     s=s%1
  321.     m=s%60
  322.     s=s//60
  323.     h=m%60
  324.     m=m//60
  325.     return right("00"h,2)":"right("00"m,2)":"right("00"s,2)
  326. /**that's all**/
  327.